//
//  LEStorageController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/15.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEStorageController.h"
#import "LEGoodStatusCell.h"
#import "LEOrderGoodsModel.h"

#import "LEAddGoodsController.h"
#import "LEEditGoodsController.h"
#import "UIViewController+Share.h"

static NSString *const LEGoodStatusCellId = @"LEGoodStatusCell";

@interface LEStorageController ()

@end

@implementation LEStorageController

- (void)setupUI
{
    [super setupUI];
    [self.tableView registerNib:[UINib nibWithNibName:@"LEGoodStatusCell" bundle:nil] forCellReuseIdentifier:LEGoodStatusCellId];
    [self initHeader];
    [self initFooter];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationRefresh:) name:AddGoods_Storage_Notification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationRefresh:) name:AddGoods_Check_Notification object:nil];
    
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)loadData
{
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    [dict setObject:@"2" forKey:@"type"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
     __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StoreGoodsListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if (error)
        {
            [self addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [self hiddenNoDataView];
            
            NSArray *tempt = datas[@"goods_list"];
            NSArray *tempModel = [LEOrderGoodsModel mj_objectArrayWithKeyValuesArray:tempt];
            
            if (weakSelf.page == 1) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:tempModel];
                
            }else{
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }
            
        }
        [weakSelf.tableView reloadData];
       
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
/**
 请求上架
 */
- (void)requestHiddenGoods:(LEOrderGoodsModel *)model
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.goods_commonid forKey:@"common_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StoreGoodsShowUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            weakSelf.page = 1;
            [weakSelf requestRefreshData];
        }
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
/**
 请求删除
 */
- (void)requestDeleteGoods:(LEOrderGoodsModel *)model
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.goods_commonid forKey:@"common_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StoreGoodsDeleteUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            weakSelf.page = 1;
            [weakSelf requestRefreshData];
        }
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEGoodStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:LEGoodStatusCellId forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    cell.status = On_Storage;
     __weak typeof (self) weakSelf = self;
    [cell goodsCellClick:^(NSInteger tag, LEOrderGoodsModel *model) {
        LELog(@"%s---%d---%ld",__func__,__LINE__,tag);
        if (tag == 1) {
            //编辑
            LELog(@"%s---%d---编辑",__func__,__LINE__);
            LEEditGoodsController *editGoods = [[LEEditGoodsController alloc] init];
            editGoods.commonid = model.goods_commonid;
            [weakSelf.navigationController pushViewController:editGoods animated:YES];
        }else if (tag == 2)
        {
            //上架
            LELog(@"%s---%d---上架",__func__,__LINE__);
            [weakSelf requestHiddenGoods:model];
            
        }else if (tag == 3)
        {
            //删除
            LELog(@"%s---%d---删除",__func__,__LINE__);
            [weakSelf showAlertView:model];

        }else if (tag == 4)
        {
            //分享
            LELog(@"%s---%d---分享",__func__,__LINE__);
            [weakSelf share:model];
        }
        
    }];

    return cell;
}
#pragma mark- 点击事件
- (void)notificationRefresh:(NSNotification *)notification
{
    [self requestRefreshData];
}

#pragma mark- privateMethod
- (void)showAlertView:(LEOrderGoodsModel *)model
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定要删除" message: nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self requestDeleteGoods:model];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark- 点击事件
- (void)share:(LEOrderGoodsModel *)model
{
    __weak typeof (self) weakSelf = self;
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        NSDictionary *locationDict = [[NSUserDefaults standardUserDefaults]objectForKey:SAVE_LOCATION];
        NSString *url = locationDict[@"url"];
        NSString *shareUrl = [NSString stringWithFormat:@"%@index.php?act=goods&op=index&goods_id=%@",url,model.goods_id];
        [weakSelf shareWebPageToPlatformType:platformType goodsUrl:shareUrl];
    }];
    
}
@end
